var latitude = null;
var longitude = null;
var coord;
document.addEventListener('plusready',function(){
	coord = plus.storage.getItem('coord');
	showWating();
	if(coord){
		coord = coord.split(',');
		console.log(coord[0]+" "+coord[1])
		gethos(coord[0],coord[1]);
	}else{
		plus.geolocation.getCurrentPosition(function (p) {
			if (!p.coords) {
			 	toast('无法获取您的位置！');
			}
			longitude = p.coords.longitude;
			latitude = p.coords.latitude;
			plus.storage.setItem('coord',longitude+','+latitude);
			gethos(longitude,latitude);
			plus.nativeUI.closeWaiting();	
		}, function () {}, {provider:'baidu', coordsType:'bd09ll', geocode:true});
	}
},false)
function gethos(longitude,latitude){
	showWating();
	$.ajax({
		type :"get",
		url :apiRoot+"?m=home&c=studio&a=getHospital",
		data :{
			latitude :latitude,
			longitude :longitude
		},
		complete :function(){
			plus.nativeUI.closeWaiting();
		},
		dataType :'json',
		success : function(data){
//			console.log(JSON.stringify(data));
			if(data.ChineseMedicine.length > 0){
				var str1 = '';
				$.each(data.ChineseMedicine, function(k,v) {
					str1 += '<div class="mui-table-view background table-view-cell home-list" data-id="'+v.aid+'" type="2">'+
								'<div>'+
									'<img src="'+webRoot+v.thumb+'" alt="" />'+
								'</div>'+
								'<div class="">'+
									'<li class="font-17">'+v.title+'</li>'+
									'<li class="color-ddd font-14">'+v.address+'</li>'+
									'<li class="color-ddd font-14">'+v.distance+'</li>'+
								'</div>'+
							'</div>';
				});
				$('#item1mobile .mui-scroll').html(str1);
			}
			if(data.WestMedicine.length > 0){
				var str2 = '';
				$.each(data.ChineseMedicine, function(k,v) {
					str2 += '<div class="mui-table-view background table-view-cell home-list" data-id="'+v.aid+'" type="109">'+
								'<div>'+
									'<img src="'+webRoot+v.thumb+'" alt="" />'+
								'</div>'+
								'<div class="">'+
									'<li class="font-17">'+v.title+'</li>'+
									'<li class="color-ddd font-14">'+v.address+'</li>'+
									'<li class="color-ddd font-14">'+v.distance+'</li>'+
								'</div>'+
							'</div>';
				});
				$('#item2mobile .mui-scroll').html(str2); 
				$('.home-list').on('tap',function(){	//医馆id
					var aid = $(this).attr('data-id');
					var type = $(this).attr('type');
					console.log(aid+" "+type);
					goNewPage('shop.html',{aid:aid, type:type})
				})
			}
		},
		error : function(e){
			console.log(JSON.stringify(e));
			errortoast();
		}
	});
}
